package com.android.zweibo.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.zweibo.ui.R;
import com.android.zweibo.util.StringUtil;

public class WBStatusAdapter extends BaseAdapter {
    private List<Status> status;
	private LayoutInflater inflater;
	private static Weibo weibo;
	
	static{
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		
		weibo = new Weibo();
		
	}
	
	public WBStatusAdapter(List<Status> status, Context context) {
		this.status = status;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return status == null ? 0:status.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return status == null ? null : status.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return status.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		return creatView(position,convertView);
	}
	
	
	private View creatView(int position, View convertView) {
		View view  ;
		if(null == convertView) {
			view  = inflater.inflate(R.layout.listview_item_template, null);
		}else {
			view = convertView;
		}
		
		bindView(position,view);
		return view;
	}

	
	public void bindView(int position, View view) {
		StaticView holder = new StaticView();
		
		Status s = status.get(position);
		
		holder.wb_item_head = (ImageView) view.findViewById(R.id.wb_item_head); //用户头像
		holder.wb_item_username = (TextView) view.findViewById(R.id.wb_item_username); //用户名称
		holder.wb_item_v = (ImageView) view.findViewById(R.id.wb_item_v); //认证用户		
		holder.wb_item_time = (TextView) view.findViewById(R.id.wb_item_time);  //发布时间
		holder.wb_item_content = (TextView) view.findViewById(R.id.wb_item_content); //微薄正文
		holder.layout = (LinearLayout) view.findViewById(R.id.chatlayout);
		holder.wb_item_content_pic = (ImageView) view.findViewById(R.id.wb_item_content_pic);// 正文中的图片
		holder.wb_item_chatcontent = (TextView) view.findViewById(R.id.wb_item_chatcontent); //微薄对话内容
		holder.wb_item_chatcontent_pic = (ImageView) view.findViewById(R.id.wb_item_chatcontent_pic); //微薄对话内容中图片
		holder.wb_item_from = (TextView) view.findViewById(R.id.wb_item_from);           //信息发布客户端
		holder.wb_item_comment = (TextView) view.findViewById(R.id.wb_item_comment);        //微薄评论数量
		holder.wb_item_redirect = (TextView) view.findViewById(R.id.wb_item_redirect);       //微薄转发数量
		
		
		//用户名
		holder.wb_item_username.setText(s.getUser().getName());
		
		//用户头像
//		SimpleImageLoder.showImage(holder.wb_item_head, s.getUser().getProfileImageURL().toString());
		
		//用户是否通过认证
		if(!s.getUser().isVerified()){
			holder.wb_item_v.setVisibility(View.GONE);
		}
		
		//发布时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = new Date(s.getCreatedAt().getTime());
		holder.wb_item_time.setText(format.format(date));
		
		//内容
		holder.wb_item_content.setText(s.getText());
		
		//是否含有图片
		if(!StringUtil.isEmpty(s.getThumbnail_pic())) {
			holder.wb_item_content_pic.setVisibility(View.VISIBLE);
//			SimpleImageLoder.showImage(holder.wb_item_content_pic,s.getThumbnail_pic());
		}
		
		//是否有转发消息
		if(null != s.getRetweeted_status()) {
			holder.layout.setVisibility(View.VISIBLE);
			holder.wb_item_chatcontent.setVisibility(View.VISIBLE);
			holder.wb_item_chatcontent.setText(s.getRetweeted_status().getText());
		}
		holder.wb_item_from.setText("信息来自：" +Html.fromHtml( s.getSource()));
		
		//评论和转发数目
		String comments = null;
		String redirects = null;
		try {
			comments = weibo.getComments(String.valueOf(s.getId())).size() + "";
			redirects = weibo.getrepostbyme(String.valueOf(s.getId())).size() + "";
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(null == comments) {
			comments = 0 + "";
		}
		if(null == redirects) {
			redirects = 0 + "" ;
		}
		holder.wb_item_comment.setText(comments);
		holder.wb_item_redirect.setText(redirects);
		
		System.out.println("comments:----"+ comments);
		System.out.println("redirects: ----"+ redirects);
		
	}
	static class StaticView{
	    ImageView wb_item_head; //用户头像
		TextView wb_item_username; //用户名称
		ImageView wb_item_v; //认证用户
		TextView wb_item_time;  //发布时间
		TextView wb_item_content; //微薄正文
		ImageView wb_item_content_pic ;// 正文中的图片
		LinearLayout layout;           //转发微薄layout
		TextView wb_item_chatcontent; //微薄对话内容
		ImageView wb_item_chatcontent_pic; //微薄对话内容中图片
		TextView wb_item_from;           //信息发布客户端
		TextView wb_item_comment;        //微薄评论数量
		TextView wb_item_redirect;       //微薄转发数量
	}
}
